<?php
//
namespace app\home\controller;

use think\Hook;
use think\Request;

class CmsController extends BaseController
{

    private $cmsCategory;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->cmsCategory = cms_cache_cat();
    }

    /*
     * 频道页
     */
    public function channel()
    {
        $cid        = $this->request->param('cid/d');
        $currentCat = $this->getCatInfo($cid);
        return $this->fetch($currentCat['cat_tpl']);
    }

    /*
     * 列表页
     */
    public function itemlist()
    {
        $cid        = $this->request->param('cid/d');
        $extendQuery = [];
        $extendSearch = ['cid'=>$cid];
        $currentCat = $this->getCatInfo($cid);
        $itemList   = cms_page_list(-1, $currentCat['mid'], $cid, '12', $extendQuery, $extendSearch, "sort desc,aid desc", false);
        if($this->request->isAjax()){
            return $itemList;
        }
        //dump($itemList);
        $this->assign('itemList',$itemList);
        $this->assign('currentCat',$currentCat);
        return $this->fetch($currentCat['cat_tpl']);
    }

    /*
     * 内容页
     */
    public function item()
    {
        $mid     = $this->request->param('mid/d', 1);
        $aid     = $this->request->param('aid/d', 1);
        $info    = cms_moudle($mid)->get($aid);
        $preNext = pre_next(-1, $mid, $aid, $info['cid']); //上下篇
        $this->getCatInfo($info['cid']);
        $this->assign('info', $info); //当前内容
        $this->assign('keyword',$info['keywords']);
        $this->assign('description',$info['description']);
        $this->assign('preInfo', $preNext['pre']); //上一篇
        $this->assign('nextInfo', $preNext['next']); //下一篇
        Hook::exec('app\\common\\behavior\\Home','cmsClick');
        return $this->fetch($info['content_tpl']);
    }

    /*
     * 搜索页
     */
    public function search()
    {
        $mid        = $this->request->param('mid/d',1);
        $extendQuery = [];
        $extendSearch = ['mid'=>$mid];
        $extendSearch['title'] = $this->request->param('title');
        if(!empty($extendSearch['title'])){
            $extendQuery['title'] = ['like',"%{$extendSearch['title']}%"];
        }
        $itemList   = cms_page_list(-1, $mid, 0, '12', $extendQuery, $extendSearch, "sort desc,aid desc", true);
        foreach ($itemList as $key=>$item){
            $itemList[$key]['title'] = str_replace($extendSearch['title'],'<span style="color:red">'.$extendSearch['title'].'</span>',$item['title']);
            $itemList[$key]['description'] = str_replace($extendSearch['title'],'<span style="color:red">'.$extendSearch['title'].'</span>',$item['description']);
        }
        if($this->request->isAjax()){
            return $itemList;
        }
        $this->assign('keyword',$this->webconfig['KEYWORDS']);
        $this->assign('description',$this->webconfig['DESCRIPTION']);
        $this->assign('itemList', $itemList);
        return $this->fetch();
    }

    /**
     * 根据栏目Id获取通用信息
     * @param $cid
     */
    private function getCatInfo($cid)
    {
        if(!is_numeric($cid)){
            $this->error('参数错误！');
        }
        $currentCat  = $this->cmsCategory[$cid]; //当前栏目
        $locationCat = cms_location($cid); //当前位置
        if ($currentCat['is_son'] == true) {
            $selfCat = cms_cat_list('self', $cid); //取当前同级
            $sonCat  = cms_cat_list('son', $cid); //取当前下级
        } else {
            $selfCat = cms_cat_list('self', $cid); //取当前同级
            $sonCat  = []; //取当前下级
        }
        $this->assign('keyword',$currentCat['cat_keywords']);
        $this->assign('description',$currentCat['cat_description']);
        $this->assign('currentCat', $currentCat); //当前栏目
        $this->assign('locationCat', $locationCat); //当前位置
        $this->assign('topCat', $locationCat[0]); //当前最顶级栏目
        $this->assign('selfCat', $selfCat); //同级栏目
        $this->assign('sonCat', $sonCat); //下级栏目
        return $currentCat;
    }
}